<?php

namespace App\Http\Controllers\Admin;

use App\Models\Role;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Validation\Rule;

class RoleController extends Controller
{
    /**
     * 角色
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function   index()
    {


        return view('admin/role/index');
    }


    /**
     * 角色添加
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function   add()
    {


        return view('admin/role/add');
    }
    /**
     * 角色编辑
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function   edit(Request $request)
    {
        $params = $request->only(['role_id']);
        $validator = \Validator::make($params, [
            'role_id'=>'required|exists:roles,id',
        ], [
            'role_id.*' => '参数错误',
        ]);

        if ($validator->fails()) {
            return $this->error($validator->errors()->first());
        }
        $role =  Role::find($request->role_id);



        return view('admin/role/edit',['data'=>$role]);
    }
    /**
     * 角色列表
     * @return \Illuminate\Http\JsonResponse
     */
    public function data(Request $request)
    {
        $searchParams = json_decode($request->searchParams, true);
        $data = Role::Orderby('id', 'desc')->Name($searchParams['name'])->paginate($request->limit)->toArray();

        return $this->success(['data'=>$data['data'], 'count'=> $data['total'],'code'=>0]);
    }


    /**
     * 详情
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function  detail(Request $request)
    {
        $params = $request->only(['role_id']);
        $validator = \Validator::make($params, [
            'role_id'=>'required|exists:roles,id',
        ], [
            'role_id.*' => '参数错误',
        ]);

        if ($validator->fails()) {
            return $this->error($validator->errors()->first());
        }
       $role =  Role::find($request->role_id);

        return $this->success($role);
    }


    /**
     * @param Request $request
     * 新增角色
     * @return \Illuminate\Http\JsonResponse
     */
      public function  addRole(Request $request)
    {
        $params = $request->only(['name', 'menu_id']);


        $validator = \Validator::make($params, [
            'name' => 'required|string|max:20|unique:roles',
//            'menu_id.*'=>'required',
        ], [
            'name.required' => '名称不能为空',
            'name.unique' => '角色已存在',
            'name.max'=>'名称长度最大为20个字符串',
//            'menu_id.*.required' => '角色权限不能为空',
        ]);
        if ($validator->fails()) {
            return $this->error($validator->errors()->first());
        }
        $data=[
            'name'=>$request->name
        ];
        //菜单id
        $menusID = $this->getMenuID($request->menu_id);
        $menus=[];
        foreach ($menusID as $item){
            $menus[]=['menu_id'=>$item];
        }

        $re = Role::add($data,$menus);
        if($re){
            return $this->success(['msg'=>'保存成功']);
        }
        return $this->error('保存失败');

    }


    /**
     * 递归获取
     * @param $data
     * @return array
     */
    public function   getMenuID($data)
    {
       static $menuID=[];

        foreach ($data as $key=>$datum){
            $menuID[]= $datum['id'];
                if(!empty($datum['children'])){

                    $this->getMenuID($datum['children']);
                }
        }
        return $menuID;

    }



    /**
     * 角色编辑
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function  saveRole(Request $request)
    {
        $params = $request->only(['role_id','name','menu_id']);

        $validator = \Validator::make($params, [
            'role_id'=>'required|exists:roles,id',
            'name'=> ['required',Rule::unique('roles')->whereNull('deleted_at')->ignore($request->role_id)],
        ], [
            'role_id.*' => '参数错误',
            'name.required' => '名称不能为空',
            'name.unique' => '已存在的角色名',
        ]);

        if ($validator->fails()) {
            return $this->error($validator->errors()->first());
        }

        //菜单id
        $menusID = $this->getMenuID($request->menu_id);
        $menus=[];
        foreach ($menusID as $item){
            $menus[]=['menu_id'=>$item];
        }

       $re =  Role::edit($request->role_id,$request->name,$menus);

        if($re){
            return $this->success(['msg'=>'保存成功']);
        }
        return $this->error('保存失败');
    }

    /**
     * 删除角色
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse\
     */
    public function  del(Request $request)
    {
        $params = $request->only(['role_id']);
        $validator = \Validator::make($params, [
            'role_id'=>'required|exists:roles,id',
        ], [
            'role_id.*' => '参数错误',
        ]);

        if ($validator->fails()) {
            return $this->error($validator->errors()->first());
        }
        $admin = Role::find($request->role_id);
        $admin->delete();
        if ($admin->trashed()) {
            return  $this->success(['msg'=>'删除成功']);
        }
        return  $this->error('删除失败');
    }


}
